﻿namespace Pub.Class
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.Remoting.Messaging;
    using System.Text;

    public class Log
    {
        private static readonly object lockHelper = new object();

        public static void AsynWrite(string fileName, string str)
        {
            AsynWrite(fileName, str, Encoding.UTF8);
        }

        public static void AsynWrite(string fileName, string str, Encoding encoding)
        {
            new Func<string, string, Encoding, bool>(Log.Write).BeginInvoke(fileName, str, encoding, iar => ((Action<string, string, Encoding>) ((AsyncResult) iar).AsyncDelegate).EndInvoke(iar), null);
        }

        public static IList<string> Read(string fileName, Encoding encoding)
        {
            string item = null;
            IList<string> list = new List<string>();
            if (FileDirectory.FileExists(fileName))
            {
                lock (lockHelper)
                {
                    using (StreamReader reader = encoding.IsNull() ? new StreamReader(fileName, encoding) : new StreamReader(fileName, encoding))
                    {
                        while ((item = reader.ReadLine()).IsNotNull())
                        {
                            list.Add(item);
                        }
                    }
                }
            }
            return list;
        }

        public static string ReadAll(string fileName, Encoding encoding)
        {
            string str = string.Empty;
            if (FileDirectory.FileExists(fileName))
            {
                lock (lockHelper)
                {
                    using (StreamReader reader = encoding.IsNull() ? new StreamReader(fileName, encoding) : new StreamReader(fileName, encoding))
                    {
                        str = reader.ReadToEnd();
                    }
                }
            }
            return str;
        }

        public static bool Write(string fileName, string str)
        {
            return Write(fileName, str, "utf-8");
        }

        public static bool Write(string fileName, string str, string encoding)
        {
            if (encoding.IsNullEmpty())
            {
                encoding = "utf-8";
            }
            return Write(fileName, str, Encoding.GetEncoding(encoding));
        }

        public static bool Write(string fileName, string str, Encoding encoding)
        {
            return WriteForStreamWriter(fileName, str, encoding);
        }

        public static bool WriteForFileStream(string fileName, string str, Encoding encoding)
        {
            bool flag = false;
            if (encoding.IsNull())
            {
                encoding = Encoding.Default;
            }
            lock (lockHelper)
            {
                using (FileStream stream = FileDirectory.FileExists(fileName) ? File.OpenWrite(fileName) : File.Create(fileName))
                {
                    byte[] bytes = encoding.GetBytes(str + Environment.NewLine);
                    stream.Seek(0L, SeekOrigin.End);
                    stream.Write(bytes, 0, bytes.Length);
                    flag = true;
                }
            }
            return flag;
        }

        public static bool WriteForStreamWriter(string fileName, string str, Encoding encoding)
        {
            bool flag = false;
            if (encoding.IsNull())
            {
                encoding = Encoding.Default;
            }
            lock (lockHelper)
            {
                using (StreamWriter writer = new StreamWriter(fileName, true, encoding))
                {
                    writer.WriteLine(str);
                    flag = true;
                }
            }
            return flag;
        }
    }
}

